<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>键盘事件</title>
    <style>
        .result{
            width: 400px;
            height: 300px;
            border:solid 1px #888;
        }
    </style>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        // 键盘事件的事件源  input textarea  window
        class App extends React.Component {
            //状态
            state = {
                msgs: [
                    'abc',
                    'xyz',
                    'opq'
                ]
            }
            render(){
                return <div>
                        <input type="text" onKeyDown={this.keydown} />
                        <hr />
                        <div className="result" ref={el => this.res = el}>
                            <ul>
                                {
                                    this.state.msgs.map((item, index) => {
                                        return <li key={index}>{item}</li> ;
                                    })
                                }
                            </ul>
                        </div>
                    </div>
            }

            keydown = (e) => {
                // 判断按下的键 是否为『回车键』
                // console.log(e.keyCode);  e.ctrlKey  用来标志 ctrl 键是否按下
                if(e.keyCode === 13 && e.ctrlKey){
                    //解构赋值
                    let {msgs} = this.state;
                    //获取输入框的值
                    let v = e.target.value;
                    //将 v 压入到 msgs 数组中
                    msgs.push(v);
                    //调用 setState
                    this.setState({
                        msgs
                    })
                    //设置文本
                    // this.res.innerHTML = v;
                }
                //a 字母
                // if(e.keyCode === 65){
                //     e.preventDefault();
                // }
            }
        }

        //渲染
        ReactDOM.render(<App />, document.querySelector('#root'));
    </script>
</body>
</html>